package com.mkyong.common.controller;

import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.mkyong.common.model.Question;
import com.mkyong.common.service.QuestionService;

@Controller
@RequestMapping("/questions")
public class QuestionsController {
    private static final Log logger = LogFactory.getLog(QuestionsController.class);
    private QuestionService questionService;

    @RequestMapping(method = RequestMethod.GET)
    public String listQuestion(@RequestParam String sort, Model model) {
        System.out.println(sort);

        Collection<Question> results = this.questionService.sortQuestions(sort);

        if (results.size() < 1) {
            return "ask";
        } else {
            // multiple owners found
            model.addAttribute("selections", results);
            return "question/questionList";
        }
    }

    @RequestMapping("/{questionId}")
    public String showQuestion(@PathVariable("questionId") int questionId, Model model) {
        model.addAttribute("question", questionService.getQuestionById(questionId));
        return "question/questionShow";
    }

    @RequestMapping("/{questionId}/edit")
    public String edit(@PathVariable("questionId") int questionId, Model model) {
        model.addAttribute("question", this.questionService.getQuestionById(questionId));
        return "question/questionEdit";
    }

    @RequestMapping(value = "/{questionId}/edit/submit", method = RequestMethod.POST)
    public String editQuestion(@ModelAttribute Question question, BindingResult result,
            SessionStatus status) {
        System.out.println("editQuestion start" + question.getId());
        this.questionService.modifyQuestion(question);
        status.setComplete();
        return "redirect:/questions/" + question.getId();
    }

    @RequestMapping(value = "ask", method = RequestMethod.GET)
    public String ask(Model model) {
        if(logger.isDebugEnabled()) {
            logger.debug("a question has bean asked");
        }
        logger.info("info:a question has bean asked ");
        Question question = new Question();
        model.addAttribute(question);
        return "question/questionForm";
    }

    @RequestMapping(value = "ask/submit", method = RequestMethod.POST)
    public String createQuestion(@ModelAttribute Question question, BindingResult result,
            SessionStatus status) {
        this.questionService.createQuestion(question);
        System.out.println("****************" + question.getTitle() + question.getContent()
                + "****************");
        status.setComplete();
        return "redirect:/questions/" + question.getId();
    }

    public void setQuestionService(QuestionService questionService) {
        this.questionService = questionService;
    }

    public QuestionService getQuestionService() {
        return questionService;
    }

}
